<script>
import {getAddColumn, getColumn, getDelColumn, getSave} from "@/utils/class/Pluponform";

export default {
  name: "colunmnPage",
  data() {
    return {
      //表单验证
      rules: {
        name: {required: true, message: '栏目名称不能为空', trigger: 'blur'},
        rank: {required: true, message: '栏目排序不能为空', trigger: 'blur'}
      },
      //删除新数组
      removeList: [],
      //添加
      dialogVisible: false,
      //编辑
      editPage: false,
      //表格
      tableData: [],
      currentType: '0',
      form: {
        name: '',
        rank: ''
      },
      //参数
      columnQuery: {
        name: '',
        sort: ''
      },
      saveQuery: {
        id: '',
        enable: '',
        sort: '',
        name: ''
      },
      delQuery: {
        ids: ''
      },
      // 分页参数
      pageQuery: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {
        });

    },
    //分页
    handleCurrentChange(val) {
      this.pageQuery.pageNum = val
      getColumn(this.pageQuery).then(res => {
        // console.log(res)
        this.tableData = res.rows
      })

    },
    //删除
    delBtn(item) {
      // console.log(item)
      this.removeList = item
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        getDelColumn(this.removeList.id).then((res) => {
          //console.log(res)
          this.getColumnList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加
    add() {
      this.dialogVisible = true
    },
    //编辑
    edit(row) {
      this.dialogVisible = true
      this.currentType = 1
      // console.log(row)
      this.saveQuery.enable = row.enable
      this.saveQuery.id = row.id
      this.form.name = row.name
      this.form.rank = row.sort
    },

    //表格
    handleClick(row) {
      console.log(row);
    },
    //添加、编辑完保存
    sure() {
      if (this.currentType === '0') {
        if (this.form.name === '' && this.form.rank === '') {
          this.$message.warning('栏目名称或栏目排序不能为空')
        } else if (this.form.name === '') {
          this.$message.warning('栏目名称不能为空')
        } else if (this.form.rank === '') {
          this.$message.warning('栏目排序不能为空')
        } else {
          this.dialogVisible = false
          this.columnQuery.name = this.form.name
          this.columnQuery.sort = this.form.rank
          getAddColumn(this.columnQuery).then(res => {
            //  console.log(res)
            this.$message.success("添加成功")
            this.getColumnList()
          })
        }


      } else {
        this.dialogVisible = false
        this.saveQuery.name = this.form.name
        this.saveQuery.sort = this.form.rank
        getSave(this.saveQuery).then(res => {
          //console.log(res)
          this.$message.success('编辑成功')
          this.getColumnList()
        })


      }

    },
    //渲染
    getColumnList() {
      getColumn().then(res => {
        //console.log(res)
        this.tableData = res.rows
      })
    }
  },
  //表格渲染
  created() {
    this.getColumnList()
  }
}
</script>

<template>
  <div>
    <div style="padding: 40px">
      <!--      添加-->
      <div class="top">
        <el-button type="primary" @click="add">添加</el-button>
        <el-dialog
          :before-close="handleClose"
          :title="currentType === '0' ? '添加' : '编辑'"
          :visible.sync="dialogVisible"
          width="40%">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="栏目名称" style="width: 400px">
              <el-input v-model="form.name" maxlength="20" placeholder="请输入栏目名称" prop="name"
                        show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="栏目排序" style="width: 400px">
              <el-input v-model="form.rank" maxlength="3" placeholder="请输入1~100的数字" prop="rank"
                        show-word-limit></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="sure">保 存</el-button>
  </span>
        </el-dialog>
      </div>
      <!--      表格-->
      <div class="main" style="margin-top: 30px">
        <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            label="序号"
            prop="sort"
            width="180">
          </el-table-column>
          <el-table-column
            label="名称"
            prop="name">
          </el-table-column>
          <el-table-column
            label="更新时间"
            prop="updateTime"
            width="200"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            prop="operate"
            width="300">
            <template slot-scope="scope">
              <el-button size="small" type="text" @click="edit(scope.row)">编辑</el-button>
              <el-button size="small" type="text" @click="delBtn(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--      分页-->
      <div class="page">
        <el-pagination
          :total="10"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
::v-deep.el-table th.el-table__cell.is-leaf, .el-table td.el-table__cell {
  text-align: center;
}

::v-deep.el-table td.el-table__cell div {
  text-align: center;
}

.page {
  margin-top: 20px;
  float: right;
}
</style>
